﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CtCommon.Utility;
using Cxx.BLL;
using Cxx.Common;
using Cxx.Entity;
using Cxx.Entity.Dto;

namespace Cxx.OrderManagerSys
{
    public partial class StackDetail : System.Web.UI.Page
    {
        public string DetailJson;

        private string _name;

        protected void Page_Load(object sender, EventArgs e)
        {
            _name = Request.QueryString["name"];
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            if (_name.IsNullOrEmptyAndTrim())
            {
                btModify.Visible = false;
            }
            else
            {
                btSave.Visible = false;
                DetailJson = Stock.Get(_name)?.JSONSerialize() ?? "";
                txtReceiver.Text = _name;
                txtReceiver.ReadOnly = true;
            }
        }

        protected void btModify_Click(object sender, EventArgs e)
        {
            string data = getData.Value;
            if (txtReceiver.Text.IsNullOrEmptyAndTrim())
            {
                txtReceiver.Msg("单位不允许为空");
                return;
            }

            if (txtStartTime.Text.IsNullOrEmptyAndTrim())
            {
                txtReceiver.Msg("请先选择时间");
                return;
            }

            List<StockInfo> oldInfos = Stock.Get(txtReceiver.Text);
            if (!data.IsNullOrEmptyAndTrim())
            {
                List<StockDto> list = data.JSONDeserialize<List<StockDto>>();
                foreach (var item in list)
                {
                    var oldInfo = oldInfos.Find(p => p.TradeName == item.TradeName);
                    if (oldInfo == null)
                    {
                        StockInfo stockInfo = new StockInfo
                        {
                            Receiver = txtReceiver.Text,
                            TotalCount = item.AddCount.ToInt32(),
                            TradeName = item.TradeName
                        };
                        Stock.Insert(stockInfo);
                        var stockRecordInfo = new StockRecordInfo
                        {
                            CreateTime = DateTime.Parse(txtStartTime.Text),
                            OrderCode = "无",
                            Receiver = txtReceiver.Text,
                            StockType = (int) StockTypes.In,
                            TotalCount = item.AddCount.ToInt32(),
                            TradeName = stockInfo.TradeName,
                            StockCode = txtStockCode.Text
                        };
                        StockRecord.Insert(stockRecordInfo);
                    }
                    else
                    {
                        var dValue = item.AddCount.ToInt32();//item.TotalCount.ToInt32() - oldInfo.TotalCount;
                        oldInfo.TotalCount = item.TotalCount.ToInt32() + dValue;
                        Stock.Update(oldInfo);
                        if (dValue == 0)
                        {
                            continue;
                        }
                        var stockRecordInfo = new StockRecordInfo
                        {
                            CreateTime = DateTime.Parse(txtStartTime.Text),
                            OrderCode = "无",
                            Receiver = txtReceiver.Text,
                            TradeName = item.TradeName,
                            StockType = dValue > 0 ? (int) StockTypes.In : (int) StockTypes.Out,
                            TotalCount = Math.Abs(dValue),
                            StockCode = txtStockCode.Text
                        };
                        StockRecord.Insert(stockRecordInfo);
                    }
                }
            }
            else
            {
                foreach (var stockInfo in oldInfos)
                {
                    StockRecordInfo stockRecordInfo = new StockRecordInfo
                    {
                        CreateTime = DateTime.Parse(txtStartTime.Text),
                        OrderCode = "无",
                        Receiver = txtReceiver.Text,
                        StockType = (int) StockTypes.Out,
                        TotalCount = stockInfo.TotalCount,
                        TradeName = stockInfo.TradeName
                    };
                    StockRecord.Insert(stockRecordInfo);
                }
            }
            JScript.Alert(Page, "修改成功", $"window.parent.location.href='http://{Request.Url.Host}:{Request.Url.Port}/StackManage.aspx';parent.layer.close(index);");
        }

        protected void Save_Click(object sender, EventArgs e)
        {
            if (txtReceiver.Text.IsNullOrEmptyAndTrim())
            {
                txtReceiver.Msg("单位不允许为空");
                return;
            }
            string data = getData.Value;
            if (!data.IsNullOrEmptyAndTrim())
            {
                List<StockDto> list = data.JSONDeserialize<List<StockDto>>();
                foreach (var item in list)
                {
                    var exInfo = Stock.Get(txtReceiver.Text, item.TradeName);
                    if (exInfo == null)
                    {
                        StockInfo info = new StockInfo
                        {
                            Receiver = txtReceiver.Text,
                            TotalCount = item.AddCount.ToInt32(),
                            TradeName = item.TradeName
                        };
                        Stock.Insert(info);
                    }
                    else
                    {
                        exInfo.TotalCount += item.AddCount.ToInt32();
                        Stock.Update(exInfo);
                    }

                    var stockRecordInfo = new StockRecordInfo
                    {
                        CreateTime = DateTime.Parse(txtStartTime.Text),
                        OrderCode = "无",
                        Receiver = txtReceiver.Text,
                        TradeName = item.TradeName,
                        TotalCount = item.AddCount.ToInt32(),
                        StockType = (int) StockTypes.In,
                        StockCode = txtStockCode.Text
                    };
                    StockRecord.Insert(stockRecordInfo);
                }
                JScript.Alert(Page, "保存成功", $"window.parent.location.href='http://{Request.Url.Host}:{Request.Url.Port}/StackManage.aspx';parent.layer.close(index);");
            }
            else
            {
                txtReceiver.Msg("没有任何可保留的信息");
            }
        }
    }
}